[message].page.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import React from 'react';
  2. import type {
  3. NextPage, GetServerSideProps, GetServerSidePropsContext,
  4. } from 'next';
  5. import { useTranslation } from 'next-i18next';
  6. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  7. import { useRouter } from 'next/router';
  8. import { NoLoginLayout } from '~/components/Layout/NoLoginLayout';
  9. import type { CommonProps } from '~/pages/utils/commons';
  10. import { getServerSideCommonProps, getNextI18NextConfig } from '~/pages/utils/commons';
  11. type Props = CommonProps;
  12. const classNames: string[] = ['login-page'];
  13. const LoginPage: NextPage<CommonProps> = () => {
  14. const { t } = useTranslation();
  15. const router = useRouter();
  16. const { message } = router.query;
  17. let loginErrorElm;
  18. const ApprovalPendingUserError = () => {
  19. return (
  20. <>
  21. <div className="alert alert-warning">
  22. <h2>{ t('login.sign_in_error') }</h2>
  23. </div>
  24. <p>Wait for approved by administrators.</p>
  25. </>
  26. );
  27. };
  28. const SuspendedUserError = () => {
  29. return (
  30. <>
  31. <div className="alert alert-warning">
  32. <h2>{ t('login.sign_in_error') }</h2>
  33. </div>
  34. <p>This account is suspended.</p>
  35. </>
  36. );
  37. };
  38. const PasswordResetOrderError = () => {
  39. return (
  40. <>
  41. <div className="alert alert-warning mb-3">
  42. <h2>{ t('forgot_password.incorrect_token_or_expired_url') }</h2>
  43. </div>
  44. <a href="/forgot-password" className="link-switch">
  45. <span className="material-symbols-outlined">key</span> { t('forgot_password.forgot_password') }
  46. </a>
  47. </>
  48. );
  49. };
  50. const DefaultLoginError = () => {
  51. return (
  52. <div className="alert alert-warning">
  53. <h2>{ t('login.sign_in_error') }</h2>
  54. </div>
  55. );
  56. };
  57. switch (message) {
  58. case 'registered':
  59. loginErrorElm = <ApprovalPendingUserError />;
  60. break;
  61. case 'suspended':
  62. loginErrorElm = <SuspendedUserError />;
  63. break;
  64. case 'password-reset-order':
  65. loginErrorElm = <PasswordResetOrderError />;
  66. break;
  67. default:
  68. loginErrorElm = <DefaultLoginError />;
  69. }
  70. return (
  71. <NoLoginLayout className={classNames.join(' ')}>
  72. <div className="mb-4 login-form-errors text-center">
  73. <div className="nologin-dialog pb-4 mx-auto">
  74. <div className="col-12">
  75. {loginErrorElm}
  76. </div>
  77. {/* If the transition source is "/login", use <a /> tag since the transition will not occur if next/link is used. */}
  78. <a href="/login">
  79. <span className="material-symbols-outlined me-1">login</span>{t('Sign in is here')}
  80. </a>
  81. </div>
  82. </div>
  83. </NoLoginLayout>
  84. );
  85. };
  86. /**
  87. * for Server Side Translations
  88. * @param context
  89. * @param props
  90. * @param namespacesRequired
  91. */
  92. async function injectNextI18NextConfigurations(context: GetServerSidePropsContext, props: Props, namespacesRequired?: string[] | undefined): Promise<void> {
  93. const nextI18NextConfig = await getNextI18NextConfig(serverSideTranslations, context, namespacesRequired);
  94. props._nextI18Next = nextI18NextConfig._nextI18Next;
  95. }
  96. export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
  97. const result = await getServerSideCommonProps(context);
  98. // check for presence
  99. // see: https://github.com/vercel/next.js/issues/19271#issuecomment-730006862
  100. if (!('props' in result)) {
  101. throw new Error('invalid getSSP result');
  102. }
  103. const props: Props = result.props as Props;
  104. await injectNextI18NextConfigurations(context, props, ['translation']);
  105. return {
  106. props,
  107. };
  108. };
  109. export default LoginPage;